#!/usr/bin/env python
# Copyright (C) 2007 by Xyhthyx <xyhthyx@gmail.com>

# This file is part of Parcelle.

# Parcelle is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

# Parcelle is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Run this script as root to uninstall Parcelle

import os
import sys
try:
	import parcelle
except ImportError:
	print 'Parcelle is not installed.'
	sys.exit(1)

if not os.environ['LOGNAME'] == 'root':
	print 'Please run this script as root.'
	sys.exit(1)

files = ['/etc/xdg/autostart/parcellestartup.desktop', '/etc/xdg/autostart/parcelle-startup.desktop', '/usr/bin/parcelle', '/usr/share/applications/parcelle.desktop', '/usr/share/parcelle/gui/about.glade', '/usr/share/parcelle/gui/parcelle.glade', '/usr/share/parcelle/gui/preferences.glade']

dirs = ['/usr/share/parcelle/gui', '/usr/share/parcelle']

# Remove files
for each in files:
	if os.path.exists(each):
		print 'Removing-> ' + each
		os.remove(each)

# Remove directories
for each in dirs:
	if os.path.exists(each):
		print 'Removing-> ' + each
		os.rmdir(each)

if os.path.exists(parcelle.__file__):
	path = os.path.dirname(parcelle.__file__)
	path_control = os.path.join(path, 'control')
	path_widgets = os.path.join(path, 'widgets')
	
	# Remove files in control
	if os.path.exists(os.path.abspath(path_control)):
		print os.listdir(path_control)
		for each in os.listdir(path_control):
			print 'Removing-> ' + os.path.join(path_control, each)
			os.remove(os.path.join(path_control, each))
	
	# Remove files in widgets
	if os.path.exists(os.path.abspath(path_widgets)):
		print os.listdir(path_widgets)
		for each in os.listdir(path_widgets):
			print 'Removing-> ' + os.path.join(path_widgets, each)
			os.remove(os.path.join(path_widgets, each))
	
	# Remove files in parcelle
	for each in os.listdir(path):
		if os.path.isfile(os.path.join(path, each)):
			print 'Removing-> ' + os.path.join(path, each)
			os.remove(os.path.join(path, each))
		elif os.path.isdir(os.path.join(path, each)):
			print 'Removing-> ' + os.path.join(path, each)
			os.rmdir(os.path.join(path, each))
	
	# Remove parcelle directory
	print 'Removing-> ' + os.path.dirname(parcelle.__file__)
	os.rmdir(os.path.dirname(parcelle.__file__))

